package com.uzai.console.service.ip138.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.feign.uzaimanage.SysRunlogWarningDto;
import com.uzai.console.service.feign.UzaiManageFeignService;
import com.uzai.console.service.ip138.Ip138ApiService;
import com.uzai.console.vo.ip138.Ip138ApiResult;
import com.uzai.console.vo.ip138.Ip138Info;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Ip128网查询ip地址:https://user.ip138.com/ip/doc
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class Ip138ApiServiceImpl implements Ip138ApiService {

    Logger logger = LoggerFactory.getLogger(Ip138ApiServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;
    @Autowired
    private UzaiManageFeignService uzaiManageFeignService;

    /**
     * 调用18.com的远程ip分析接口查询ip信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param ip
     * @return null
     */
    public Ip138Info findIpInfo(String ip){
        Ip138Info ip18Info = new Ip138Info();
        ip18Info.setIp(ip);
        try {
            String ip138ApiUrl = uzaiConsoleSysConfig.getIp138ApiUrl();
            String ip138ApiToken = uzaiConsoleSysConfig.getIp138ApiToken();
            String url = ip138ApiUrl + "?ip=" + ip + "&token=" + ip138ApiToken;
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            if (response!=null){
                //远程调用失败，获取msg异常信息，根据自己的业务进行合理处理
                Ip138ApiResult ip138ApiResult = JSONObject.parseObject(response.getBody(), Ip138ApiResult.class);
                 if(response.getStatusCode() == HttpStatus.OK){
                     if(ip138ApiResult != null && "ok".equals(ip138ApiResult.getRet())){
                         String[] data = ip138ApiResult.getData();
                         if (data != null && data.length == 7){
                             ip18Info.setCountry(data[0]);
                             ip18Info.setProvince(data[1]);
                             ip18Info.setCity(data[2]);
                             ip18Info.setArea(data[3]);
                             ip18Info.setOperator(data[4]);
                             ip18Info.setMailcode(data[5]);
                             ip18Info.setAreacode(data[6]);
                         }else{
                             logger.error("--请求IP138接口获取ip信息返回结果格式错误-msg={}", "返回的data数组长度不正确");
                             SysRunlogWarningDto sysRunlogWarningDto = new SysRunlogWarningDto();
                             sysRunlogWarningDto.setText("ip138.com(131x0183)接口异常：返回的data数组长度不正确");
                             UzaiRespVo<Object> objectUzaiRespVo = uzaiManageFeignService.sysRunlogWarning(sysRunlogWarningDto);
                             logger.info("--调用官方钉钉报警消息接口---para={},result={}",JSONObject.toJSONString(sysRunlogWarningDto), JSONObject.toJSONString(objectUzaiRespVo));
                         }
                     }else{
                         logger.error("--请求IP138接口获取ip信息异常-msg={}",ip138ApiResult.getMsg());
                         SysRunlogWarningDto sysRunlogWarningDto = new SysRunlogWarningDto();
                         sysRunlogWarningDto.setText("ip138.com(131x0183)接口异常：" + ip138ApiResult.getMsg());
                         UzaiRespVo<Object> objectUzaiRespVo = uzaiManageFeignService.sysRunlogWarning(sysRunlogWarningDto);
                         logger.info("--调用官方钉钉报警消息接口---para={},result={}",JSONObject.toJSONString(sysRunlogWarningDto), JSONObject.toJSONString(objectUzaiRespVo));
                     }
                 }else{
                     logger.error("--请求IP138接口获取ip信息异常-msg={}",ip138ApiResult.getMsg());
                     SysRunlogWarningDto sysRunlogWarningDto = new SysRunlogWarningDto();
                     sysRunlogWarningDto.setText("ip138.com(131x0183)接口异常：" + ip138ApiResult.getMsg());
                     UzaiRespVo<Object> objectUzaiRespVo = uzaiManageFeignService.sysRunlogWarning(sysRunlogWarningDto);
                     logger.info("--调用官方钉钉报警消息接口---para={},result={}",JSONObject.toJSONString(sysRunlogWarningDto), JSONObject.toJSONString(objectUzaiRespVo));
                 }
            }
        } catch (Exception e) {
            logger.error("--请求IP138接口获取ip异常-msg={}", e.getMessage());
            SysRunlogWarningDto sysRunlogWarningDto = new SysRunlogWarningDto();
            sysRunlogWarningDto.setText("ip138.com(131x0183)接口异常：" + e.getMessage());
            UzaiRespVo<Object> objectUzaiRespVo = uzaiManageFeignService.sysRunlogWarning(sysRunlogWarningDto);
            logger.info("--调用官方钉钉报警消息接口---para={},result={}",JSONObject.toJSONString(sysRunlogWarningDto), JSONObject.toJSONString(objectUzaiRespVo));
        }
        return ip18Info;
    }
}
